//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#if _MSC_VER > 1000
#pragma once
#endif

#ifndef __HELPER_H__
#define __HELPER_H__

#include <elasys_server.h>
#include "tlsmanager.h"
#include "cthread.h"
#include "cprocess.h"

#ifdef _DEBUG
#if defined(_GNUC)
#define FixMe(statement) Aura_printf("[FixMe]<%s>"#statement"\n", __FUNCTION__);
#elif defined(_MSVC)
#define FixMe(statement) Aura_printf("[FixMe]<%s>"##statement##"\n", __FUNCTION__);
#else
#error unsupported compiler.
#endif
#else
#define FixMe(statement)
#endif

#define LOCK_START(hLock) do { AuraCriticalSection_Enter(hLock); } while(0)
#define LOCK_END(hLock)   do { AuraCriticalSection_Leave(hLock); } while(0)

#define LOCK   LOCK_START(m_hLock)
#define UNLOCK LOCK_END(m_hLock)

#define THREAD_START_SLEEP() \
    do { \
        CThread * pCurThread = NULL; \
        pCurThread = static_cast<CThread *>(Helper_Thread_GetCurrent()); \
        pCurThread->Helper_SetState(ThreadState_Sleeping); \
        do

#define THREAD_END_SLEEP  \
        while(0); \
        pCurThread->Helper_SetState(ThreadState_Running); \
    }while(0)

#define GET_CPROCESS() static_cast<CProcess *>(Helper_Process_GetCurrent())
#define CTHREAD(pThread) static_cast<CThread *>(pThread)

// Helper for Pseudo Kernel.
ECode Helper_PKernel_Initialize();

void  Helper_PKernel_Destroy();

// Helper for Process, Thread.

IProcess * Helper_Process_GetCurrent();


// Helper for thread.

Boolean Helper_Thread_IsMain(
        /* [in] */ IThread * pThread);

IThread * Helper_Thread_GetMain();

Boolean Helper_Thread_IsCurrent(
        /* [in] */ IThread * pThread);

IThread *  Helper_Thread_GetCurrent();

void Helper_Thread_SetCurrent(
        /* [in] */ IThread * pThread);


// Helper for Tls Manager.
ECode Helper_TlsMgr_AcquireSlot(
        /* [in] */ ThreadLocalSlotType type,
        /* [out] */ Int32 * pSlotIndex);

ECode Helper_TlsMgr_RelingquishSlot(
        /* [in] */ Int32 slotIndex,
        /* [out] */ BufferOf<Int32> * pUsedPtrs);

ECode Helper_TlsMgr_CleanupSlot(
        /* [in] */ Int32 slotIndex,
        /* [out] */ BufferOf<Int32> * pUsedPtrs);

ECode Helper_TlsMgr_GetValue(
        /* [in] */ ThreadLocalSlotType type,
        /* [in] */ Int32 slotIndex,
        /* [out] */ PVoid * ppValue);

ECode Helper_TlsMgr_SetValue(
        /* [in] */ ThreadLocalSlotType type,
        /* [in] */ Int32 slotIndex,
        /* [in] */ PVoid pValue);


// Helper for Tls Zone.

ECode Helper_TlsZone_Create(
        /* [out] */ CTlsZone ** ppTlsZone);

ECode Helper_TlsZone_Clone(
        /* [in] */ CTlsZone * pTemplate,
        /* [out] */ CTlsZone ** ppTlsZone);

void  Helper_TlsZone_Destroy(
        /* [in] */ CTlsZone * pTlsZone);

CTlsZone * Helper_TlsZone_GetCurrent();

ECode Helper_TlsZone_SetCurrent(
        /* [in] */ CTlsZone * pThreadTls);


#endif //!(__HELPER_H__)

